﻿using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;
using Rio.Framework.Localisation;

namespace Rio.Framework.Pages.Viewer
{
    public partial class ScanInstall : ChildWindow
    {
        public ScanInstall()
        {
            InitializeComponent();
            InitLabel();
            CheckScanClientInstalled();
        }

        /// <summary>
        /// Translation
        /// </summary>
        public void InitLabel()
        {
                    LabelInstalled.Text = string.Format("{0}:", Resx.GetLabel(Constantes.k_SCAN_INSTALLED_TEXT));
                    ScanOptionTrue.Content = Resx.GetLabel(Constantes.k_YES);
                    ScanOptionFalse.Content = Resx.GetLabel(Constantes.k_NO);
                    LabelDownloadScan.Text = string.Format("{0}:", Resx.GetLabel(Constantes.k_SCAN_DOWNLOAD_TEXT));
                    DownloadButton.Content = Resx.GetLabel(Constantes.k_DOWNLOAD);
                    OKButton.Content = Resx.GetLabel(Constantes.k_OK);
                    CancelButton.Content = Resx.GetLabel(Constantes.k_CANCEL);
                    this.Title = Resx.GetLabel(Constantes.k_SCAN_INSTALLED);
        }

        /// <summary>
        /// This method initialize the radio buttons
        /// </summary>
        private void CheckScanClientInstalled()
        {
            BusinessEntitiesDomainContext l_oBusinessEntitiesDomainContext = new BusinessEntitiesDomainContext();
            l_oBusinessEntitiesDomainContext.GetPcApplicationStatus("RIOSCAN", delegate(InvokeOperation<ReturnBoolean> retr)
            {
                
                if (retr!=null && retr.Value!= null && retr.Value.IsValid)
                {
                    if (retr.Value.Value)
                    {
                        ScanOptionTrue.IsChecked = true;
                    }
                    else
                    {
                        ScanOptionFalse.IsChecked = true;
                    }
                }
            }
            , null);

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            BusinessEntitiesDomainContext l_oBusinessEntitiesDomainContext = new BusinessEntitiesDomainContext();
            l_oBusinessEntitiesDomainContext.UpdatePcApplicationStatus("RIOSCAN", ScanOptionTrue.IsChecked.Value);
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// This event is called when clicking on the download button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            SettingsDomainContext l_oSettingsDS = new SettingsDomainContext();
            l_oSettingsDS.GetScanMSI(delegate(InvokeOperation<string> str)
            {
                LoadMSI(str.Value);
            }, null);

        }

        private void LoadMSI(string p_sScanMSIPath)
        {
            System.UriBuilder uriBuilder = new UriBuilder(System.Windows.Browser.HtmlPage.Document.DocumentUri.Scheme,
                                                          System.Windows.Browser.HtmlPage.Document.DocumentUri.Host,
                                                          System.Windows.Browser.HtmlPage.Document.DocumentUri.Port,
                                                          "RioWeb/Rio.Application.FileDownload.aspx")
                                               {
                                                   Query =
                                                       "fp=" + p_sScanMSIPath +
                                                       "&name=Rio.Application.Scanx86.Setup.msi"
                                               };

            System.Windows.Browser.HtmlPage.Window.Navigate(uriBuilder.Uri, "_blank");
        }
    }
}

